package cn.interestingshop.service.classify;


import cn.interestingshop.dao.ClassifyVoDao;
import cn.interestingshop.dao.ClassifyVoDaoImpl;
import cn.interestingshop.entity.Classify;
import cn.interestingshop.entity.Goods;
import cn.interestingshop.utils.ClassifyVo;
import cn.interestingshop.utils.DataSourceUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ClassifyServiceImpl implements ClassifyService{



    @Override
    public List<ClassifyVo> selectAll() {

        Connection conn=null;
        List<ClassifyVo> list=new ArrayList<>();

        try {
            conn= DataSourceUtil.openConnection();
            ClassifyVoDao classifyDao=new ClassifyVoDaoImpl(conn);

            list=classifyDao.selectAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return list;
    }

    @Override
    public List<Classify> selectAllon(Integer currentPageNo, Integer pageSize) {
        Connection conn=null;
        List<Classify> list=new ArrayList<>();

        try {
            conn= DataSourceUtil.openConnection();
            ClassifyVoDao classifyDao=new ClassifyVoDaoImpl(conn);

            list=classifyDao.selectAllon(currentPageNo,pageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return list;
    }

    @Override
    public int selectAllCount() {
        Connection conn=null;
        int rs=0;

        try {
            conn= DataSourceUtil.openConnection();
            ClassifyVoDao classifyDao=new ClassifyVoDaoImpl(conn);

            rs=classifyDao.selectAllCount();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return rs;
    }

    @Override
    public int deleteById(int id) {
        Connection conn=null;
        int rs=0;

        try {
            conn= DataSourceUtil.openConnection();
            ClassifyVoDao classifyDao=new ClassifyVoDaoImpl(conn);

            rs=classifyDao.deleteById(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return rs;
    }

    @Override
    public Classify selectOne(int id) {
        Connection conn=null;
        Classify classify=new Classify();

        try {
            conn= DataSourceUtil.openConnection();
            ClassifyVoDao classifyDao=new ClassifyVoDaoImpl(conn);

            classify=classifyDao.selectOne(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return classify;
    }

    @Override
    public List<Classify> selectOneType(int type) {
        Connection conn=null;
        List<Classify> classify=new ArrayList<>();

        try {
            conn= DataSourceUtil.openConnection();
            ClassifyVoDao classifyDao=new ClassifyVoDaoImpl(conn);

            classify=classifyDao.selectOneType(type);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return classify;
    }

    @Override
    public int update(int id, String classifyName, int classifyLevel1, int classifyLevel2) {

        Connection conn=null;
        int rs=0;

        try {
            conn= DataSourceUtil.openConnection();
            ClassifyVoDao classifyDao=new ClassifyVoDaoImpl(conn);

            rs=classifyDao.update(id,classifyName,classifyLevel1,classifyLevel2);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return rs;
    }

    @Override
    public int insert(int type, String classifyName, int classifyLevel1, int classifyLevel2) {
        Connection conn=null;
        int rs=0;

        try {
            conn= DataSourceUtil.openConnection();
            ClassifyVoDao classifyDao=new ClassifyVoDaoImpl(conn);

            rs=classifyDao.insert(type,classifyName,classifyLevel1,classifyLevel2);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return rs;
    }


}
